package com.test.springboot.springboot3aiollama1.controller;

import com.test.springboot.springboot3aiollama1.service.AiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： liuziyang
 * @date： 2025/2/7-16:54
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Controller
public class AiController {
  private static final Logger LOGGER = LoggerFactory.getLogger(AiController.class);
  private AiService aiService;

  @Autowired
  public void setAiService(AiService aiService) {
    this.aiService = aiService;
  }

  @GetMapping("/main")
  public String main() {
    return "main";
  }

  @PostMapping("/ai/generate")
  public String generate(@RequestParam("prompt") String prompt, Model model) {
    if (null == prompt || prompt.isEmpty()) {
      LOGGER.info("prompt is empty");
    } else {
      LOGGER.info("receive prompt from client:{}", prompt);
      try {
        model.addAttribute("result", aiService.chat(prompt));
      } catch (Exception e) {
        model.addAttribute("result", e.getMessage());
      }
    }
    return "main";
  }
}
